package com.service.impl;

import com.entity.PageBean;
import com.entity.PurOrder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.PurOrderMapper;
import com.service.PurOrderService;
import com.vo.PurOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.Random;

@Service
public class PurOrderServiceImpl implements PurOrderService {
    @Autowired
    private PurOrderMapper purOrderMapper;


    @Override
    public PageBean<PurOrder> getOrderList(Integer pageIndex, Integer pageSize, PurOrderVO purOrderVO) {
        PageHelper.startPage(pageIndex, pageSize);
        List<PurOrder> list= purOrderMapper.selectOrderList(purOrderVO);
        PageInfo<PurOrder> pageInfo = new PageInfo<>(list);
        PageBean<PurOrder> pageBean = new PageBean<>(
                pageIndex,//第几页
                pageSize,//每页显示的条数
                pageInfo.getPages(),//总页数
                (int)pageInfo.getTotal(),//数据总条数
                pageInfo.getList()//当前页的数据集合
        );
        return pageBean;
    }

//    @Override
//    public String getOrdernum() {
//        //获取当前时间的年份、月份、目期、时分秒
//        Calendar calendar = Calendar.getInstance();
//        int year = calendar.get( calendar. YEAR);
//        int month = calendar.get(calendar. MONTH) +1;
//        int day = calendar.get(Calendar. DAY_OF_MONTH);
//        int hour = calendar.get(calendar.HOUR_OF_DAY);
//        int minute = calendar.get(calendar. MINUTE);
//        int second = calendar.get(calendar.SECOND);
//        //生成随机数
//        int random = new Random( ).nextInt( 10000 ) ;
//        //计算校验位
//        int checkCode = (year + month + day + hour + minute + second + random) % 10;
//        //拼接各个部分，生成最终订单号
//        String orderWo = String.format("%04d%02d%02d%02d%02d%02d%04d%d", year,month,day,hour, minute,second,random, checkCode);
//        return orderWo;
//    }

    @Override
    public void addOrderList(PurOrder purOrder) {
//        //获取当前时间的年份、月份、目期、时分秒
//        Calendar calendar = Calendar.getInstance();
//        int year = calendar.get( calendar. YEAR);
//        int month = calendar.get(calendar. MONTH) +1;
//        int day = calendar.get(Calendar. DAY_OF_MONTH);
//        int hour = calendar.get(calendar.HOUR_OF_DAY);
//        int minute = calendar.get(calendar. MINUTE);
//        int second = calendar.get(calendar.SECOND);
//        //生成随机数
//        int random = new Random( ).nextInt( 10000 ) ;
//        //计算校验位
//        int checkCode = (year + month + day + hour + minute + second + random) % 10;
//        //拼接各个部分，生成最终订单号
//        String orderWo = String.format("%04d%02d%02d%02d%02d%02d%04d%d", year,month,day,hour, minute,second,random, checkCode);
//        purOrder.setOrdernum(orderWo);
        purOrderMapper.insertOrderList(purOrder);
    }

    @Override
    public void updateOrderstatus(PurOrder purOrder) {
        purOrderMapper.updateOrderstatus(purOrder);
    }

    @Override
    public void updateOrderList(PurOrder purOrder) {
        purOrderMapper.updateOrderList(purOrder);
    }

    @Override
    public void removeOrderByOid(int oid) {
        purOrderMapper.cancleOrderByOid(oid);
    }



}
